import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:qgchat/common/class/upload/upload.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/resModel.dart';

class ReleaseVideoDynamic extends StatefulWidget {
  ReleaseVideoDynamic({Key key}) : super(key: key);

  @override
  _ReleaseVideoDynamicState createState() => _ReleaseVideoDynamicState();
}

class _ReleaseVideoDynamicState extends State<ReleaseVideoDynamic> {
  final TextEditingController textCtrl = TextEditingController();
  String videoPath;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: col("f5f7fa"),
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(px(88)),
        child: AppBar(
          centerTitle: true,
          title: Text(
            "发布视频动态",
            style: TextStyle(
              fontSize: px(34),
              fontWeight: FontWeight.bold,
            ),
          ),
          actions: <Widget>[
            Container(
              width: px(125),
              child: FlatButton(
                onPressed: () async {
                  final text = textCtrl.text;
                  if (text.length > 256) {
                    showToast("文字最多256个字符");
                    return;
                  }
                  if (videoPath == null) {
                    showToast("未上传视频");
                    return;
                  }

                  CommonApis.senddongtai(text, video: videoPath).then((res) {
                    showToast(res.msg);
                    if (res.status == 1) {
                      Global.noCtxRoute.navigateBack();
                    }
                  });
                },
                padding: EdgeInsets.zero,
                child: Text(
                  "确认",
                  style: TextStyle(
                    color: col("5640c6"),
                    fontSize: px(34),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            // 输入框
            Container(
              color: Colors.white,
              child: CupertinoTextField(
                controller: textCtrl,
                maxLength: 256,
                placeholder: '记录此刻的心情吧',
                placeholderStyle: TextStyle(
                  color: col("bfc2cc"),
                  fontSize: px(34),
                ),
                maxLines: 8,
                padding: EdgeInsets.symmetric(
                  horizontal: px(30),
                  vertical: px(20),
                ),
                decoration: BoxDecoration(
                  border: null,
                ),
              ),
            ),
            // 分割线
            Container(
              padding: EdgeInsets.symmetric(horizontal: px(30)),
              color: Colors.white,
              child: Container(
                height: 1,
                color: col("edeff2"),
              ),
            ),
            // 上传图片组件
            _UploadVideoBtn(videoNot: (url) {
              videoPath = url;
            }),
          ],
        ),
      ),
    );
  }
}

class _UploadVideoBtn extends StatefulWidget {
  final Function(String) videoNot;
  _UploadVideoBtn({this.videoNot, Key key}) : super(key: key);

  @override
  __UploadVideoBtnState createState() => __UploadVideoBtnState();
}

class __UploadVideoBtnState extends State<_UploadVideoBtn> {
  String url;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: px(690),
      margin: EdgeInsets.all(px(30)),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(
          color: col("edeff2"),
          width: 1,
        ),
      ),
      child: FlatButton(
        onPressed: () async {
          try {
            final file = await Upload.choiceVideo();
            url = await Upload.qiniuUploadVideo(context, file);
            widget.videoNot(url);
            setState(() {});
          } catch (e) {
            if (e is String) toast(e);
          }
        },
        padding: EdgeInsets.zero,
        child: url == null ? _AddIcon() : _Vframe(src: url),
      ),
    );
  }
}

class _AddIcon extends StatelessWidget {
  const _AddIcon({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Icon(
      Icons.add,
      color: col("bfc2cc"),
      size: px(100),
    );
  }
}

class _Vframe extends StatelessWidget {
  final String src;
  const _Vframe({this.src, Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colors.black,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Image.network("$src?vframe/jpg/offset/0/h/690"),
          Icon(
            Icons.refresh,
            color: Colors.white,
            size: px(100),
          ),
        ],
      ),
    );
  }
}
